/*
 * ConfigPanel.java
 *
 * Created on 27. august 2008, 16:47
 */

package jturbofile.gui;

import javax.swing.JFileChooser;
import jturbofile.Config;

/**
 *
 * @author  Rolf
 */
public class ConfigPanel extends javax.swing.JPanel {

    private boolean ready;
    
    /** Creates new form ConfigPanel */
    public ConfigPanel() {
        ready = false;
        
        initComponents();
        
        Config conf = Config.singleton();
        
        jDownloadTo.setText(conf.getValue("path"));
        jCurrentSpeed.setText(conf.getSpeed()+"");
        jSpeed.setValue(conf.getSpeed());
        
        jCompressionType.removeAllItems();
        for(Config.Compression c : Config.Compression.values()) {
            jCompressionType.addItem(c);
            if( c == conf.getCompressionType() ) {
                jCompressionType.setSelectedItem(c);
                ready = true;
            }
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel2 = new javax.swing.JLabel();
        jDownloadTo = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jSpeed = new javax.swing.JSlider();
        jCurrentSpeed = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jCompressionType = new javax.swing.JComboBox();

        jLabel1.setText("Config");

        jLabel2.setText("Download To");

        jDownloadTo.setEditable(false);

        jButton1.setText("Browse");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel3.setText("Buffer Size");

        jSpeed.setMaximum(512);
        jSpeed.setMinimum(4);
        jSpeed.setMinorTickSpacing(4);
        jSpeed.setPaintTicks(true);
        jSpeed.setSnapToTicks(true);
        jSpeed.setValue(16);
        jSpeed.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jSpeedStateChanged(evt);
            }
        });

        jCurrentSpeed.setText("jLabel4");

        jLabel4.setText("CompressionType:");

        jCompressionType.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jCompressionType.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jCompressionTypeItemStateChanged(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addContainerGap(359, Short.MAX_VALUE))
            .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jSpeed, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 241, Short.MAX_VALUE)
                    .addComponent(jDownloadTo, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 241, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton1)
                    .addComponent(jCurrentSpeed))
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jCompressionType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(241, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jButton1)
                    .addComponent(jDownloadTo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(15, 15, 15)
                        .addComponent(jLabel3))
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jSpeed, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jCurrentSpeed))))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jCompressionType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(155, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        JFileChooser fileBrowser = new JFileChooser();
        fileBrowser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        fileBrowser.setFileHidingEnabled(false);
        fileBrowser.setMultiSelectionEnabled(false);
        int value = fileBrowser.showDialog(null, "Select download directory");
        if(value == JFileChooser.APPROVE_OPTION) {
            jDownloadTo.setText(fileBrowser.getSelectedFile().getPath());
            Config conf = Config.singleton();
            conf.setValue("path", fileBrowser.getSelectedFile().getPath());
        }
}//GEN-LAST:event_jButton1ActionPerformed

private void jSpeedStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jSpeedStateChanged
    Config conf = Config.singleton();
    
    conf.setSpeed(jSpeed.getValue());
    jCurrentSpeed.setText(""+jSpeed.getValue());
}//GEN-LAST:event_jSpeedStateChanged

private void jCompressionTypeItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jCompressionTypeItemStateChanged
    if( ready ) {
        Config conf = Config.singleton();

        conf.setCompressionType((Config.Compression)jCompressionType.getSelectedItem());

        System.out.println("Test: "+jCompressionType.getSelectedItem());
    }
}//GEN-LAST:event_jCompressionTypeItemStateChanged


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JComboBox jCompressionType;
    private javax.swing.JLabel jCurrentSpeed;
    private javax.swing.JTextField jDownloadTo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSlider jSpeed;
    // End of variables declaration//GEN-END:variables

}
